package com.huluobo.a2206douyindemo.adapter;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.live.MessageEntity;

/**
 * Created by LC on 2024/8/27.
 */
public class ChatRoomAdapter extends BaseQuickAdapter<MessageEntity, BaseViewHolder> {
    public ChatRoomAdapter() {
        super(R.layout.item_char_room);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, MessageEntity message) {
        String username = message.getUsername();
        String content = message.getContent();
        String msg = username + ":" + content;

//        <span color = "red" > 张三 </span ><span color = "blue" > 发送了一条信息 </span >
        //构建一个Span富文本编辑器
        SpannableStringBuilder builder = new SpannableStringBuilder(msg);

        //定义2个颜色
        ForegroundColorSpan blue = new ForegroundColorSpan(Color.BLUE);
        ForegroundColorSpan yellow = new ForegroundColorSpan(Color.YELLOW);
        ForegroundColorSpan red = new ForegroundColorSpan(Color.RED);

        //颜色,从哪个位置开始标记,标记结束的位置,是否包含前后元素
        if (username.equals("系统")) {
            builder.setSpan(red, 0, message.getUsername().length() + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        } else {
            builder.setSpan(blue, 0, message.getUsername().length() + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        builder.setSpan(yellow, message.getUsername().length() + 1, msg.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        baseViewHolder.setText(R.id.item_chat_room_tv, builder);

    }
}
